package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * xxl-job定时任务执行器
 * @author JIAN
 */
@Slf4j
@Component
@SuppressWarnings("unused")
public class SpringCacheSyncHandler {
    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync() {
        log.info(">>>>>>>>开始进行缓存同步，更新已启用区域");

        // 清理开通服务区域缓存
        redisTemplate.delete(RedisConstants.CacheName.JZ_CACHE + "::ACTIVE_REGIONS");
        // 更新开通服务区域缓存
        List<RegionSimpleResDTO> regionList = regionService.queryActiveRegionListCache();

        // 更新每个区域对应的缓存
        regionList.forEach(region -> regionService.refreshRegionRelateCaches(region.getId()));

        log.info(">>>>>>>>缓存同步结束，完成更新已启用区域");
    }

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private IRegionService regionService;
}